﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformNet;
using System.IO;
using System.Data.OleDb;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_GridConfig : Form
	{
		private Form_MainSet parent = null;

		private BaseCoreNet baseCoreNet = null;

		private string section = "GridConfig";			// 多语言文件配置节名称

		private string tip = "提示";						// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_GridConfig(Form_MainSet parent, BaseCoreNet baseCoreNet)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.parent = parent;
			this.baseCoreNet = baseCoreNet;

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "gridConfig.Text");
			inputGridExcelButton.Text = LanguageOper.ReadValue(section, "inputGridExcelButton.Text");
			downloadGridButton.Text = LanguageOper.ReadValue(section, "downloadGridButton.Text");
			closeButton.Text = LanguageOper.ReadValue(section, "closeButton.Text");
		}

		// 关闭窗体
		private void closeButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		// 导入网格数据文件
		private void inputGridExcelButton_Click(object sender, EventArgs e)
		{
			OpenFileDialog o = new OpenFileDialog();
			o.Filter = "Excel Files(*.xlsx;*.xls)|*.xlsx;*.xls";
			o.Multiselect = false;
			if (Directory.Exists(parent.lastPath_GridConfig_InputGridExcel))
			{
				o.InitialDirectory = parent.lastPath_GridConfig_InputGridExcel;
			}
			else
			{
				o.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
			}
			DialogResult dr = o.ShowDialog();
			if (dr == DialogResult.OK)
			{
				string filePath = o.FileName;
				
				if (!File.Exists(filePath))
				{
					return;
				}

				DataTable dataTable = readExcelData(filePath);
				if (dataTable != null)
				{
					int effectRowNum = 0;
					int effectColumnNum = 0;
					for (int i = 1; i < dataTable.Columns.Count; i++)
					{
						if (!dataTable.Rows[0].ItemArray[i].ToString().Trim().Equals(""))
						{
							effectColumnNum++;
						}
					}
					for (int i = 1; i < dataTable.Rows.Count; i++)
					{
						if (!dataTable.Rows[i].ItemArray[0].ToString().Trim().Equals(""))
						{
							effectRowNum++;
						}
					}

					try
					{
						int[,] tmpGridConfigValue = new int[effectRowNum, effectColumnNum];

						for (int rowIndex = 1; rowIndex <= effectRowNum; rowIndex++)
						{
							for (int columnIndex = 1; columnIndex <= effectColumnNum; columnIndex++)
							{
								string valueStr = dataTable.Rows[rowIndex].ItemArray[columnIndex].ToString().Trim();
								if (valueStr.Equals(""))
								{
									valueStr = "0";
								}
								tmpGridConfigValue[rowIndex - 1, columnIndex - 1] = int.Parse(valueStr);
							}
						}

						baseCoreNet.gridConfigValue = new int[effectRowNum, effectColumnNum];
						for (int rowIndex = 1; rowIndex <= effectRowNum; rowIndex++)
						{
							for (int columnIndex = 1; columnIndex <= effectColumnNum; columnIndex++)
							{
								baseCoreNet.gridConfigValue[rowIndex - 1, columnIndex - 1] = tmpGridConfigValue[rowIndex - 1, columnIndex - 1];
							}
						}

						gridExcelTextBox.Text = filePath;
						gridExcelTextBox.SelectionStart = gridExcelTextBox.Text.Length;
						gridExcelTextBox.ScrollToCaret();

						string subMsg1 = "Excel数据导入完成";
						if (LanguageOper.available == true)
						{
							subMsg1 = LanguageOper.ReadValue(section, "msgStr.0");
						}
						string subMsg2 = string.Format("有效行数 = {0}, 有效列数 = {1}", effectRowNum, effectColumnNum);
						if (LanguageOper.available == true)
						{
							subMsg2 = string.Format(LanguageOper.ReadValue(section, "msgStr.1"), effectRowNum, effectColumnNum);
						}
						string msg = subMsg1 + "\n\n" + subMsg2;
						MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
					catch (Exception)
					{
						gridExcelTextBox.Text = "";

						string msg = "Excel数据解析错误, 请检查";
						if (LanguageOper.available == true)
						{
							msg = LanguageOper.ReadValue(section, "msgStr.2");
						}
						
						MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
						
						return;
					}

					parent.lastPath_GridConfig_InputGridExcel = (new FileInfo(o.FileName)).DirectoryName;
				}
			}
		}

		// 读取 Excel 数据
		private DataTable readExcelData(string filePath)
		{
			string fileType = System.IO.Path.GetExtension(filePath);
			using (DataSet ds = new DataSet())
			{
				string strCon = string.Format("Provider=Microsoft.{0}.OLEDB.{1}.0;Extended Properties=\"Excel {2}.0;HDR={3};IMEX=1;\";data source={4};",
					(fileType == ".xls" ? "Jet" : "ACE"), (fileType == ".xls" ? 4 : 12), (fileType == ".xls" ? 8 : 12), "NO", filePath);
				string strCom = "SELECT * FROM [Sheet1$]";
				using (OleDbConnection myConn = new OleDbConnection(strCon))
				{
					using (OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn))
					{
						myConn.Open();
						myCommand.Fill(ds);
					}
					if (ds == null || ds.Tables.Count <= 0)
					{
						string msg = "Excel数据解析错误, 请检查";
						if (LanguageOper.available == true)
						{
							msg = LanguageOper.ReadValue(section, "msgStr.2");
						}

						MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

						return null;
					}
					else
					{
						return ds.Tables[0];
					}
				}
			}
		}
	}
}
